<template>
  <div class="single-list-filter-wrapper" @click.stop="clickBlank()" v-show="isVisible">
    <div class="top-text-wrapper" @click.stop="showSingleListDialog()">
      <p class="left-select-text">
        <span v-if="leftTipText&&leftTipText.length>0">{{leftTipText}}&nbsp;</span>
        {{placeHolderText}}
      </p>
      <div class="arrow-down-img" :class="{'arrow-up-img': showDropDown}"/>
    </div>
    <div class="drop-down-wrapper" v-show="showDropDown">
      <!--<el-scrollbar style="height:230px;">-->
      <ul>
        <li v-for="(item,index) in singleListData" @click.stop="selectCurrentSingleObject(item,index)"
            class="li-single-filter-wrapper">
          <p class="single-filter-object-show-text"
             :class="{'selected-single-filter': currentSelectPosition == index}">
            {{item.selectName}}</p>
        </li>
      </ul>
      <!--</el-scrollbar>-->

    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import TopFilter from "../filter/top-filter1.vue";
  import {bus} from "./js/bus.js";

  const TAG = "singleListFilter";
  export default {
    components: {},
    props: {
      defaultText: {
        type: String,
        default() {
          return "请选择";
        },
      },
      pageChange: {
        type: Number,
        default: null
      },
      filter: {
        type: Object,
        default: null
      },
      type: { // 用于判断当前使用的是哪个筛选项，例如统筹区或者是年份，可选项：planArea(统筹区),years(年份)
        type: String,
        default() {
          return "";
        }
      },
      type1: {
        type: String,
        default: null
      },
      leftTipText: {
        type: String,
        default() {
          return "";
        }
      }
    },
    created() {
      bus.$on('pageChange', () => {
        console.log('pageChange......uid===' + this._uid)
        setTimeout(() => {
          this.selectCurrentSingleObject(this.singleListData[0], 0)
        }, 0)
      })
      if (this.type == "insuredTypeSe1") {
        this.currentSelectPosition = 2;
      }
      bus.$on("showDropDownDialog", (emitTag) => {
        if (emitTag == this._uid) {
          // 显示待选项
          this.showDropDown = !this.showDropDown;
        } else {
          this.showDropDown = false;
        }
      });
    },
    data() {
      return {
        placeHolderText: this.defaultText,
        currentSelectPosition: 0,
        showDropDown: false,
        singleListData: [],
        isVisible: true
      };
    },
    methods: {
      setIsVisible(visible) {
        this.isVisible = visible;
      },
      selectCurrentSingleObject(item, index) {
        // 选择列表项目
        if (this.type != "freeSortType" && this.type != "insuredType" && this.type != "insuredTypeSe1") {
          this.placeHolderText = item.selectName;
        }
        this.currentSelectPosition = index;
        this.$emit("confirmSingleSelect", item.selectId, item.selectName);
        this.showDropDown = false;
      },
      showSingleListDialog() {
        // 通过发送时间消息显示当前下拉框，关闭其他下拉框
        bus.$emit("showDropDownDialog", this._uid);
      },
      //点击确定是传回选中的id值
      clickConfirm() {
        let chooseList = [];
        this.filter.nc.insuredType.map((item, index) => {
          if (item.checked && item.selectId && item.selectId.length > 0) {
            chooseList.push(item.selectId);
          }
        });
        this.$emit("confirmSingleSelect", chooseList);
        this.showDropDown = false;
      },
      clickBlank() {
      },
      changeSingleListData(type, selectYear) {
        if (type == "planArea") {
          this.singleListData = this.filter.nc.planArea;
        } else if (type == "years") {
          let years = [];
          // 获取当前年份和月份
          let myDate = new Date();
          let curYear = myDate.getFullYear();
          let curMonth = myDate.getMonth() + 1;
          if (curMonth == 1) {
            curYear = curYear - 1;
          }
          this.filter.years.forEach((item) => {
            if (item <= curYear) {
              years.push({
                selectId: item,
                selectName: item
              });
            }
          });
          this.singleListData = years;
          this.placeHolderText = this.singleListData[0].selectName;
        } else if (type == "freeSortType") {
          this.singleListData = this.filter.nc.freeSortType;
          // this.placeHolderText = this.singleListData[0].selectName
          this.placeHolderText = "总费用";
        } else if (type == "insuredType") {
          this.singleListData = this.filter.nc.insuredType;
          /*this.placeHolderText = this.singleListData[0].selectName*/
          this.placeHolderText = "医疗类别";
        } else if (type == "hospitalRankType") {
          this.singleListData = this.filter.nc.hospitalRankType;
          this.placeHolderText = this.singleListData[0].selectName;
        } else if (type == "insuredTypeSe1") {
          this.singleListData = this.filter.nc.insuredTypeSecond;
          /*this.placeHolderText = this.singleListData[2].selectName*/
          this.placeHolderText = "医疗类别";
        } else if (type == "insuredTypeSe") {
          this.singleListData = this.filter.nc.insuredTypeSecond;
          this.placeHolderText = this.singleListData[0].selectName;
        } else if (type == "month") {
          // 获取当前年份和月份
          let myDate = new Date();
          let curYear = myDate.getFullYear();
          let curMonth = myDate.getMonth() + 1;
          console.log("当前年份和月份：" + curYear + " " + curMonth);
          let endMonth = 12;
          if (selectYear == curYear) {
            if (curMonth == 1) {
              // 因为当前月份为1月，没有统计报告，故隐藏月份
              this.setIsVisible(false);
              return;
            } else {
              endMonth = curMonth - 1;
            }
          } else {
            endMonth = 12;
          }
          let month = [];
          for (let i = 1; i <= endMonth; i++) {
            month.push({
              selectId: i,
              selectName: i
            });
          }
          this.singleListData = month;
          this.placeHolderText = this.singleListData[0].selectName;
        } else if (type == "reportType") {
          let reportType = null;
          // 获取当前年份和月份
          let myDate = new Date();
          let curYear = myDate.getFullYear();
          let curMonth = myDate.getMonth() + 1;
          console.log("当前年份和月份：" + curYear + " " + curMonth + " " + selectYear);
          // 根据当前年份和月份，动态变换报告类型
          if (selectYear == curYear) {
            if (curMonth == 1) {
              // 因为当前月份为1月，没有统计报告，故隐藏月份
              this.setIsVisible(false);
              return;
            }
            if (curMonth < 4) {
              reportType = [
                {selectId: 1, selectName: "月报"}
              ];
            } else if (curMonth < 7) {
              reportType = [
                {selectId: 1, selectName: "月报"},
                {selectId: 2, selectName: "季报"}
              ];
            } else if (curMonth < 10) {
              reportType = [
                {selectId: 1, selectName: "月报"},
                {selectId: 2, selectName: "季报"},
                {selectId: 3, selectName: "半年报"}
              ];
            } else if (curMonth < 13) {
              reportType = [
                {selectId: 1, selectName: "月报"},
                {selectId: 2, selectName: "季报"},
                {selectId: 3, selectName: "半年报"}
              ];
            }
          } else {
            reportType = [
              {selectId: 1, selectName: "月报"},
              {selectId: 2, selectName: "季报"},
              {selectId: 3, selectName: "半年报"},
              {selectId: 4, selectName: "年报"}
            ];
          }
          this.singleListData = reportType;
          this.placeHolderText = this.singleListData[0].selectName;
        } else if (type == "season") {
          let season = null;
          // 获取当前年份和月份
          let myDate = new Date();
          let curYear = myDate.getFullYear();
          let curMonth = myDate.getMonth() + 1;
          console.log("当前年份和月份：" + curYear + " " + curMonth);
          // 根据当前年份和月份，动态变换报告类型
          if (selectYear == curYear) {
            if (curMonth < 4) {
              season = [];
            } else if (curMonth < 7) {
              season = [
                {selectId: 1, selectName: "一季度"}
              ];
            } else if (curMonth < 10) {
              season = [
                {selectId: 1, selectName: "一季度"},
                {selectId: 2, selectName: "二季度"}
              ];
            } else if (curMonth < 13) {
              season = [
                {selectId: 1, selectName: "一季度"},
                {selectId: 2, selectName: "二季度"},
                {selectId: 3, selectName: "三季度"}
              ];
            }
          } else {
            season = [
              {selectId: 1, selectName: "一季度"},
              {selectId: 2, selectName: "二季度"},
              {selectId: 3, selectName: "三季度"},
              {selectId: 4, selectName: "四季度"}
            ];
          }
          this.singleListData = season;
          this.placeHolderText = this.singleListData[0].selectName;
        } else if (type == "halfYear") {
          let halfYear = [
            {
              selectId: 1,
              selectName: "上半年"
            },
            {
              selectId: 2,
              selectName: "下半年"
            }
          ];
          this.singleListData = halfYear;
          this.placeHolderText = this.singleListData[0].selectName;
        } else if (type == "socialInsurance") {
          this.singleListData = this.filter.nc.insuranceTypes;
          this.placeHolderText = this.singleListData[0].selectName;
        } else if (type == "siYearInterval") {
          // 获取社保年度
          let siYearInterval = [];
          // 获取当前年份和月份
          let myDate = new Date();
          let curYear = myDate.getFullYear();
          let curMonth = myDate.getMonth() + 1;
          let curSiYear;
          if (curMonth >= 7) {
            curSiYear = curYear - 1;
          } else {
            curSiYear = curYear - 2;
          }
          for (let i = curSiYear; i >= curSiYear - 3; i--) {
            siYearInterval.push({
              selectId: i,
              selectName: i
            });
          }
          this.singleListData = siYearInterval;
          this.placeHolderText = this.singleListData[0].selectName;
        } else if (type == "workStatus") {
          this.singleListData = this.filter.nc.workStatus;
          this.placeHolderText = this.singleListData[0].selectName;
        }
      },
      setCurrentSelectPosition(index) {
        if (this.type != "freeSortType" && this.type != "insuredType" && this.type != "insuredTypeSe1") {
          if (this.singleListData && this.singleListData.length > 0 && index >= 0) {
            this.currentSelectPosition = index;
            this.placeHolderText = this.singleListData[index].selectName;
          }
        }
      },
      setSingleListData(data, index) {
        // 设置数据源
        this.singleListData = data;
        if (!index) {
          index = 0;
        }
        if (this.singleListData && this.singleListData.length > 0 && index >= 0) {
          this.currentSelectPosition = index;
          this.placeHolderText = this.singleListData[index].selectName;
        }
      },
      getSingleListData() {
        return this.singleListData;
      }
    },
    mounted() {
      document.querySelector("body").addEventListener("click", () => {
        this.showDropDown = false;
      });
      if (this.filter) {
        this.changeSingleListData(this.type);
      }
      // console.log(this.type + ' single-without-box:' + JSON.stringify(this.singleListData))
    },
    watch: {
      pageChange(){
        this.currentSelectPosition = 0;
        if (this.filter) {
          this.changeSingleListData(this.type);
        }
      },
      filter(val) {
        // console.log("single-list-filter-without-checkbox  filter发生变化");
        if (val) {
          this.changeSingleListData(this.type);
        }
      }
    }

  };
</script>

<style lang="scss">
  @import "topFilter.css";

  .single-list-filter-wrapper .el-scrollbar__wrap {
    overflow: auto;
  }
</style>
